package com.whl215.kit.adapter.element

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.whl215.kit.adapter.GrapeViewHolder

/**
 * author： happiness
 * date： 2021/4/15
 * description：
 */
abstract class GrapeElement {
    var currCode: Int = -1
    var onErrorListener: OnErrorListener? = null
    fun buildViewHolder(parent: ViewGroup): GrapeViewHolder {
        val holder = GrapeViewHolder(getViewBinding(parent))
        if (onErrorListener != null) {
            holder.itemView.setOnClickListener {
                if (currCode == GrapeAction.LIST_ERROR || currCode == GrapeAction.LOAD_MORE_ERROR)
                    onErrorListener?.onError()
            }
        }
        return holder
    }

    abstract fun getViewBinding(parent: ViewGroup): ViewBinding

    abstract fun bindViewHolder(holder: GrapeViewHolder, action: GrapeAction)

    interface OnErrorListener {
        fun onError()
    }
}